//
//  LibRecView.swift
//  WalkNovel
//
//  Created by Kevin on 2021/11/4.
//

import UIKit

class LibRecReusableView: UICollectionReusableView {

    lazy var scrollView: UIScrollView = {
        let scro = UIScrollView.init(frame: .zero)
        scro.showsHorizontalScrollIndicator = true
        scro.showsVerticalScrollIndicator = false
        return scro
    }()
    
    lazy var libLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 16)
        label.textColor = .black
        label.text = "我的書架"
        return label
    }()
    
    lazy var allBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setImage(UIImage(named: "book_read"), for: .normal)
        btn.setImage(UIImage(named: "book_read"), for: .highlighted)
        btn.rx.tap.bind { [weak self] in
            let vc = WNLibraryAllVC()
            vc.hidesBottomBarWhenPushed = true
            self?.viewContainingController?.navigationController?.pushViewController(vc, animated: true)
        }.disposed(by: rx.disposeBag)
        return btn
    }()
    
    var recViewArr = [LibRecBookView]()

    var libArr: [BookDetailModel]? {
        didSet {
            if let arr = libArr {
                for item in scrollView.subviews {
                    item.removeFromSuperview()
                }
                recViewArr.removeAll()
                guard arr.count > 0 else { return }
                for i in 0..<arr.count {
                    let view = LibRecBookView()
                    view.frame = CGRect(x: CGFloat(i)*(scaleWidth(61)+20)+20, y: 0, width: scaleWidth(61), height: scaleWidth(90)+30)
                    view.model = arr[i]
                    recViewArr.append(view)
                    scrollView.addSubview(view)
                }
                scrollView.contentSize = CGSize(width: CGFloat(arr.count)*(scaleWidth(61)+20)+20, height: 0)
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(scrollView)
        addSubview(allBtn)
        addSubview(libLabel)
        scrollView.snp.makeConstraints { (make) in
            make.top.equalTo(16)
            make.left.right.equalTo(0)
            make.height.equalTo(scaleWidth(90)+30)
        }
        allBtn.snp.makeConstraints { (make) in
            make.bottom.equalTo(0)
            make.right.equalTo(-20)
        }
        libLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(allBtn.snp.centerY)
            make.left.equalTo(20)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class LibRecBookView: UIView {
    
    lazy var bookBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.rx.tap.bind { [weak self] in
            self?.viewContainingController?.wn_pushBookVC(self?.model?.bookId ?? "")
        }.disposed(by: rx.disposeBag)
        btn.imageView?.contentMode = .scaleAspectFill
        btn.wn_cornerWithBorder(2)
        btn.clipsToBounds = true
        return btn
    }()
    
    lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 10)
        label.textColor = .black
        label.numberOfLines = 2
        return label
    }()
    
    var model: BookDetailModel? {
        didSet {
            if let m = model {
                bookBtn.wn_setupImg(m.coverUrl, placeHolder: "placeholder_cover_ver")
                nameLabel.text = m.name
            }
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(bookBtn)
        addSubview(nameLabel)
        bookBtn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(0)
            make.size.equalTo(CGSize(width: scaleWidth(61), height: scaleWidth(90)))
        }
        nameLabel.snp.makeConstraints { (make) in
            make.left.equalTo(bookBtn.snp.left)
            make.top.equalTo(bookBtn.snp.bottom).offset(2)
            make.right.equalTo(bookBtn.snp.right)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
